<?php
class CustomerManageController extends CAdmin{
	function __construct(){
		parent::__construct();
	}
	
	public function init(){
		$cache = new Caches();
		$option_cache = $cache->getcache('option', 'commons');
		include self::admin_tpl('customer_list');
	}
	
	//客户选择
	public function search(){
		$cache = new Caches();
		$option_cache = $cache->getcache('option', 'commons');
		include self::admin_tpl('customer_search');
	}
	
	public function ajx_init(){
		$cond = '';
		//搜索条件处理
		$pro = new CProcess();
		$cond = $pro->getSearchFromPost($_POST);
		
		$customerdb = new Customer_Model();
		$page = isset($_GET['page']) ? intval($_GET['page']) : '1';
		$userid = $_SESSION['userid'];
		$pagesize = 2;	
		$setype = ROUTE_C;
		$where ="customer_id in (select crmid from r_crmentity where (smcreatorid = '$userid' or smsharerid ='$userid') and setype = '$setype' and deleted = 0)".$cond;
		$datas = $customerdb->listinfo($where,'customer_id,customer_name,hotmemo,customer_type,customer_source,phone', '', $page, $pagesize);
		$cache = new Caches();
		$option_cache = $cache->getcache('option', 'commons');
		$page_data = array();
		foreach ($datas as $data) {
			$data['hotmemo'] = $option_cache['hotmemo'][$data['hotmemo']];
			$data['customer_type'] = $option_cache['customer_type'][$data['customer_type']];
			$data['customer_source'] = $option_cache['customer_source'][$data['customer_source']];
			$page_data[] = $data;
		}
		$head = "customer_name,hotmemo,customer_type,customer_source,phone";
		$page_head = explode(',',$head);
		$page_state = $customerdb->pages; 		
		$page = array('page_data'=>$page_data,'page_head'=>$page_head,'page_state'=>$page_state);
		echo $infot = json_encode($page);
	}
	
	public function add(){
		if(isset($_POST['dosubmit'])){
			$customerdb = new Customer_Model();
			$crmentitydb = new CrmEntity_Model();
			$usergroupdb = new User_group_Model();
			$info = $this->checkinfo($_POST['info']);
			$fields = array('customer_name','hotmemo','customer_type','customer_source','address','phone','email','website','fax','remark');
			foreach ($info as $k => $v) {
				if(!in_array($k, $fields)){
					unset($info[$k]);
				}
			}
			$new_customer_id = $customerdb->insert($info,true);
			$userid = $_SESSION['userid'];
			$setype = ROUTE_C;
			$parentid=$usergroupdb->get_one(array('userid'=>$userid),'parentid');//查找是否有组长
			if($parentid){
				$crmentitydb->insert(array('crmid'=>$new_customer_id,'smcreatorid'=>$userid,'smsharerid'=>$parentid['parentid'],'setype'=>$setype,'createdtime'=>SYS_TIME));	
			} else {
				$crmentitydb->insert(array('crmid'=>$new_customer_id,'smcreatorid'=>$userid,'setype'=>$setype,'createdtime'=>SYS_TIME));
			}			
			self::showmessage('操作成功！','?m=account&c=customermanage');
		} else{
			$cache = new Caches();
			$option_cache = $cache->getcache('option', 'commons');
			include self::admin_tpl('customer_add');
		}
	}
	
	public function edit(){
		if(isset($_POST['dosubmit'])){
			$customerdb = new Customer_Model();
			$crmentitydb = new CrmEntity_Model(); 
			$info = $this->checkinfo($_POST['info']);
			$customer_id = intval($info['customer_id']);
			$userid = $_SESSION['userid'];
			$fields = array('customer_name','hotmemo','customer_type','customer_source','address','phone','email','website','fax','remark');
			foreach ($info as $k => $v) {
				if(!in_array($k, $fields)){
					unset($info[$k]);
				}
			}
			$customerdb->update($info,array('customer_id'=>$customer_id));
			$crmentitydb->update(array('modifiedby'=>$userid,'modifiedtime'=>SYS_TIME),array('crmid'=>$customer_id,'setype'=>ROUTE_C));
			self::showmessage('操作成功！','?m=account&c=customermanage');
		} else {
			$cache = new Caches();
			$option_cache = $cache->getcache('option', 'commons');
			$customerdb = new Customer_Model();
			$info = $customerdb->get_one(array('customer_id'=>$_GET['customer_id']));
			extract($info);
			include self::admin_tpl('customer_edit');
		}
	}
	
	public function delete(){
		$customer_id = $_POST['customer_id'];
		$userid = $_SESSION['userid'];
		$crmentitydb = new CrmEntity_Model();
		$crmentitydb->update(array('deletedby'=>$userid,'deletedtime'=>SYS_TIME,'deleted'=>1),array('crmid'=>$customer_id,'setype'=>ROUTE_C));
	}
	
	private function checkinfo($info){
		$validator = new GlobalValidator();
		if(!is_array($info)){
			self::showmessage('参数不完整'); return false;
		}elseif(empty($info['customer_name'])){
			self::showmessage('客户名不能空');
		}elseif(!$this->checkname($info['customer_name'],$info['y_customer_name'])){
			self::showmessage('客户已存在！');
		} elseif(empty($info['email']) || !$validator->is_email($info['email'])){
			self::showmessage('email不合法'); return false;
		}
		return $info;
	}
	
	private function checkname($name,$yname){
		$customer_name = trim($name);
		$y_customer_name = $yname;
		$customerdb = new Customer_Model();
		if($customerdb->get_one("customer_name = '$customer_name' and customer_name not in ('$y_customer_name')",'customer_id')){
			return false;
		}		
		return true;
	}
	
	public function public_checkcustomername_ajx(){
		$customerdb = new Customer_Model();
		$customer_name = $_GET['customer_name'];
		$y_customer_name = $_GET['y_customer_name'];
		if($customerdb->get_one("customer_name = '$customer_name' and customer_name not in ('$y_customer_name')",'customer_id')){
			exit('0');
		}
		exit('1');
	}
}
